** Estimate the pass-through with different assumptions about spillovers using data from the ACS, using poor counties 
** Summarize relevant variables: Weighted share of wages earned by MW affected workers
** JHL

*************************************
** Set up workspace
*************************************
version 14.0
clear all
set more off

cd "${path_home}"
adopath + ../programs

** log using "${path_log}/a03_acs_mw_shares_0615_06q1", text replace

*************************************
** Start work here
*************************************

timer clear
timer on 1 

*************************************
** [1] Merge in AMS spillover estimates, weight shares by those estimates to get theoretical estimates of pass-through 
*************************************

use "${path_dta}/acs/mwshares_2006", clear
foreach y of numlist 2007/2015 {
	merge m:1 id using ${path_dta}/acs/mwshares_`y', nogen 
}

** Merge reg results from AMS
rename id p
merge m:1 p using "${path_raw}/ams/regressions-1979-2012-all_marg", nogen 

* Cumulative shares, share earned by workers up to the i'th percentile  
foreach v of varlist nat* i* poor* rich* {
	cap gen r`v'=.
	* Allow all the way to the 100th percentile? Tried, robust
	foreach i of numlist 1/100 {
		* qui replace r`v'=`v'[`i']/`v'[100] in `i'
		qui replace r`v'=`v'[`i']/`v'[99] in `i'
	}
}

* Share densities, share earned by workers at the i'th percentile 
foreach v of varlist rnat* ri* rpoor* rrich* {
	cap gen d`v'=`v'[_n]-`v'[_n-1]
	qui replace d`v'=`v' if p==1 
}

* Levels FD, 2SLS 
	* Could also check against Lee (1999)
gen t1=b1_marg/se1_marg 
gen t4=b4_marg/se4_marg
gen t6=b6_marg/se6_marg

* Normalize by max or (average binding percentile or 5th percentile)
	* Max gives most conservative bound 
	
	* Specification 1: AMS Levels 2SLS
	qui su b4_marg
	gen w1=b4_marg/`r(max)'

	* Specification 2: Lee (1999) 
	qui su b1_marg	
	gen w2=b1_marg/`r(max)'

reshape long mwshare_nat_@ nat_pct_@ nat_tot_@ i4451_pct_@ i4451_tot_@ i4451_nat_count_@ mwshare_i4451_@ ///
i44611_pct_@ i44611_tot_@ i44611_nat_count_@ mwshare_i44611_@ ///
i45211_pct_@ i45211_tot_@ i45211_nat_count_@ mwshare_i45211_@ ///
i722Z_pct_@ i722Z_tot_@ i722Z_nat_count_@ mwshare_i722Z_@ ///
rnat_pct_@ rnat_tot_@ ri4451_pct_@ ri4451_tot_@ ///
ri44611_pct_@ ri44611_tot_@ ///
ri45211_pct_@ ri45211_tot_@ ///
ri722Z_pct_@ ri722Z_tot_@ ///
drnat_pct_@ drnat_tot_@ dri4451_pct_@ dri4451_tot_@ ///
dri44611_pct_@ dri44611_tot_@ ///
dri45211_pct_@ dri45211_tot_@ ///
dri722Z_pct_@ dri722Z_tot_@ /// 
mwshare_nat_q2_2q_@ poor_pct_@ poor_tot_@ poor_count_@ i4451_poor_pct_@ i4451_poor_tot_@ i4451_poor_count_@ mwshare_i4451_poor_@ ///
i44611_poor_pct_@ i44611_poor_tot_@ i44611_poor_count_@ mwshare_i44611_poor_@ ///
i45211_poor_pct_@ i45211_poor_tot_@ i45211_poor_count_@ mwshare_i45211_poor_@ ///
i722Z_poor_pct_@ i722Z_poor_tot_@ i722Z_poor_count_@ mwshare_i722Z_poor_@ ///
rpoor_pct_@ rpoor_tot_@ ri4451_poor_pct_@ ri4451_poor_tot_@ ri44611_poor_pct_@ ri44611_poor_tot_@ ///
ri45211_poor_pct_@ ri45211_poor_tot_@ ri722Z_poor_pct_@ ri722Z_poor_tot_@ ///
drpoor_pct_@ drpoor_tot_@ dri4451_poor_pct_@ dri4451_poor_tot_@ dri44611_poor_pct_@ dri44611_poor_tot_@ ///
dri45211_poor_pct_@ dri45211_poor_tot_@ dri722Z_poor_pct_@ dri722Z_poor_tot_@ ///
mwshare_nat_q1_2q_@ rich_pct_@ rich_tot_@ rich_count_@ i4451_rich_pct_@ i4451_rich_tot_@ i4451_rich_count_@ mwshare_i4451_rich_@ ///
i44611_rich_pct_@ i44611_rich_tot_@ i44611_rich_count_@ mwshare_i44611_rich_@ ///
i45211_rich_pct_@ i45211_rich_tot_@ i45211_rich_count_@ mwshare_i45211_rich_@ ///
i722Z_rich_pct_@ i722Z_rich_tot_@ i722Z_rich_count_@ mwshare_i722Z_rich_@ ///
rrich_pct_@ rrich_tot_@ ri4451_rich_pct_@ ri4451_rich_tot_@ ri44611_rich_pct_@ ri44611_rich_tot_@ ///
ri45211_rich_pct_@ ri45211_rich_tot_@ ri722Z_rich_pct_@ ri722Z_rich_tot_@ ///
drrich_pct_@ drrich_tot_@ dri4451_rich_pct_@ dri4451_rich_tot_@ dri44611_rich_pct_@ dri44611_rich_tot_@ ///
dri45211_rich_pct_@ dri45211_rich_tot_@ dri722Z_rich_pct_@ dri722Z_rich_tot_@, i(p) j(year) 

* Generate weighted shares: Multiply shares by percentile with spillover estimates  
foreach i in 4451 44611 45211 722Z {
	foreach s of numlist 1/2 {
	
		cap gen dri`i'_w`s'_tot=dri`i'_tot_*w`s'		
		cap gen dri`i'_w`s'_rich_tot=dri`i'_rich_tot_*w`s'
		cap gen dri`i'_w`s'_poor_tot=dri`i'_poor_tot_*w`s'
	
	}
}

* Average weighted shares over 8 years, by specification 
foreach i in 4451 44611 45211 722Z  {
	di `y' "`i'"
	local j=1
	foreach y of numlist 2006/2015 {	
		foreach s of numlist 1/2 {
			
			* National 
			cap gen avg_dri`i'_w`s'_tot=.
			qui total dri`i'_w`s'_tot if year==`y'
			mat b=e(b)
			qui replace avg_dri`i'_w`s'_tot=b[1,1] in `j'

			* Rich 
			cap gen avg_dri`i'_w`s'_rich_tot=.
			qui total dri`i'_w`s'_rich_tot if year==`y'
			mat b=e(b)
			qui replace avg_dri`i'_w`s'_rich_tot=b[1,1] in `j'

			* Poor
			cap gen avg_dri`i'_w`s'_poor_tot=.
			qui total dri`i'_w`s'_poor_tot if year==`y'
			mat b=e(b)
			qui replace avg_dri`i'_w`s'_poor_tot=b[1,1] in `j'
		
		}
	local ++j 	
	}
}

** Weighted share of wages earned by MW affected workers: No spillovers 
	** All counties, rich counties, poor counties 
** 44611 45211 722Z 	
foreach i in 4451 {
	di "`i'" 
	su mwshare_i`i'_ 
	su mwshare_i`i'_rich_ 		
	su mwshare_i`i'_poor_
}

** Weighted share of wages earned by MW affected workers: No spillovers
	** 44611 45211 722Z 
** Table uses Spec 1 and 3	
foreach i in 4451 {
	foreach s of numlist 1/2 {
	
		di "`i' `s'" 
		
		su avg_dri`i'_w`s'_poor_tot
		su avg_dri`i'_w`s'_rich_tot
		su avg_dri`i'_w`s'_tot


	}
}

*************************************
** Close workspace
*************************************
timer off 1
timer list 1
** log close